package cn.golxen.blog.controller;

import cn.golxen.blog.commons.utils.Result;
import cn.golxen.blog.entity.Category;
import cn.golxen.blog.exception.exceptionClass.AddCategoryException;
import cn.golxen.blog.exception.exceptionClass.DeleteCategoryException;
import cn.golxen.blog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @PostMapping(value = "/admin/add")
    public Result addCategory(@RequestBody Category category){
        Integer integer=categoryService.addCategory(category);
        if (integer!=0)
            return Result.successResult();
        else
            throw new AddCategoryException();
    }
    @GetMapping(value = "/getlist")
    public Result getCategories(){
        List<String> list=categoryService.getCategories();
        return Result.successResult(list);
    }
    @GetMapping(value = "/admin/delete/{category}")
    public Result deleteCategory(@PathVariable(value = "category") String category){
        Integer integer=categoryService.deleteCategory(category);
        if (integer!=0)
            return Result.successResult();
        else
            throw new DeleteCategoryException();
    }

}
